package com.addressBook.tools;

/**
 *
 * @author Lam
 */
import com.addressBook.bean.UserInfo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/**
 * 只解决3.0版本
 *
 * @author Lam
 */
public class Converter {

    /**
     * 主要是分析由文件生成的字符串生成一个bean
     *
     * @param beansString
     * @return
     */
    public static UserInfo createBeans(String beansString) {

//        System.out.print(beansString);
        String wordString = "";
        UserInfo userBean = new UserInfo();
        userBean.setEmpty(false);
        Boolean flag = new Boolean(true);
        //分析元素;
        while (flag) {
            wordString = beansString.substring(0, beansString.indexOf("\n") + 1);
//            System.out.println(wordString);
            if (wordString.startsWith("N:")) {
                String str = wordString.substring(wordString.indexOf("N:") + 2);
                str = str.substring(0, str.length() - 1);
//                String[] list = str.split(";");
//                String nameString = "";
//                for (int i = 0; i < list.length; i++) {
//                    nameString = nameString + list[i];
//                }
                userBean.setN(str);
                System.out.println(str);
//                System.out.println(wordString);

            }
//            System.out.println("aaa");
            if (wordString.startsWith("FN:")) {

                String str = wordString.substring(0, wordString.length() - 1);
                String fnString = str.substring(str.indexOf(":") + 1);
                userBean.setFN(fnString);
                System.out.println(fnString);
            }
            if (wordString.startsWith("ORG")) {
                String str = wordString.substring(0, wordString.length() - 1);
                String orgString = str.substring(str.indexOf(":") + 1);
                userBean.setORG(orgString);
                System.out.println(orgString);
            }
            if (wordString.startsWith("TITLE")) {
                String str = wordString.substring(0, wordString.length() - 1);
                String titleString = str.substring(str.indexOf(":") + 1);
                userBean.setTITLE(titleString);
                System.out.println(titleString);
            }
            if (wordString.startsWith("EMAIL")) {
                String str = wordString.substring(0, wordString.length() - 1);
                String emailString = str.substring(str.indexOf(";") + 1);
                userBean.getEMAIL().add(emailString);
                System.out.println(emailString);
            }
            if (wordString.startsWith("TEL")) {
                String str = wordString.substring(0, wordString.length() - 1);
                String telString = str.substring(str.indexOf(";") + 1);
                userBean.getTEL().add(telString);
                System.out.println(telString);
            }
            if (wordString.startsWith("NOTE")) {
                String str = wordString.substring(0, wordString.length() - 1);
                String noteString = str.substring(str.indexOf(":") + 1);
                userBean.setNOTE(noteString);
//                System.out.println(wordString);
                System.out.println(beansString);
//                System.out.println(noteString);
            }
            if (wordString.startsWith("CATEGORIES")) {
                String str = wordString.substring(0, wordString.length() - 1);
                String categoriesString = str.substring(str.indexOf(":") + 1);
                userBean.setCATEGORIES(categoriesString);
                System.out.println(categoriesString);
            }
            if (wordString.startsWith("BDAY")) {
                String str = wordString.substring(0, wordString.length() - 1);
                String bdayString = str.substring(str.indexOf(":") + 1);
                userBean.setBRITHDAY(bdayString);
                System.out.println(bdayString);
            }
            if (wordString.startsWith("PHOTO")) {
//                String urlString = "C:\\Users\\Administrator\\Desktop\\" + userBean.getN() + ".jpg";
                String str = beansString.substring(beansString.indexOf(":") + 1);
                str = str.substring(0, str.indexOf(":"));
                str = str.substring(0, str.lastIndexOf("\n"));
                System.out.println(str);
                str = str.replaceAll("\n", " ");
                str = str.replaceAll(" ", "").trim();
//                try {
//                    CommonPhotoTools.BASE64Decoding(str, "jpeg", urlString);
//                } catch (Exception ex) {
//                    System.out.println("图片解释出错");
//                    Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                userBean.setPHOTO(urlString);
                userBean.setPHOTO(str);
            }
            if (wordString.contains("END:VCARD")) {
                System.out.println("endendend");
                flag = false;
            }
            beansString = beansString.replace(wordString, "");
            beansString = beansString.trim();
            beansString = beansString + "\n";
//            System.out.println(beansString);
//            if (beansString.contains("\n")) {
//                System.out.println("+++++++++");
//            }
        }
        return userBean;
    }

    public static String bean2String(UserInfo bean) throws Exception {
        String beanString = "";
        beanString = beanString + "BEGIN:VCARD" + "\n" + "VERSION:3.0" + "\n";
        if (!bean.getN().isEmpty()) {
            beanString = beanString + "N:" + bean.getN() + ";;;;" + "\n";
        }
        if (!bean.getFN().isEmpty()) {
            beanString = beanString + "FN:" + bean.getFN() + "\n";
        }
        if (bean.getTEL().size() > 0) {
            ArrayList<String> telList = bean.getTEL();
            for (int j = 0; j < telList.size(); j++) {
                beanString = beanString + "TEL;" + telList.get(j) + "\n";
            }
        }
        if (bean.getEMAIL().size() > 0) {
            ArrayList<String> emaiList = bean.getEMAIL();
            for (int j = 0; j < emaiList.size(); j++) {
                beanString = beanString + "EAMIL;" + emaiList.get(j) + "\n";
            }
        }
        if (!bean.getBRITHDAY().isEmpty()) {
            beanString = beanString + "BDAY:" + bean.getBRITHDAY() + "\n";
        }
        if (!bean.getORG().isEmpty()) {
            beanString = beanString + "ORG;CHARSET=UTF-8:" + bean.getORG() + "\n";
        }
        if (!bean.getTITLE().isEmpty()) {
            beanString = beanString + "TITLE;CHARSET=UTF-8:" + bean.getTITLE() + "\n";
        }
        if (!bean.getCATEGORIES().isEmpty()) {
            beanString = beanString + "CATEGORIES;CHARSET=UTF-8:" + bean.getCATEGORIES() + "\n";
        }

        if (!bean.getPHOTO().isEmpty()) {
            String type = bean.getPHOTO().substring(0, 3);
            if (type.matches("/9j/")) {
                type = "JPEG:";
            } else {
                type = "PNG:";
            }
            String photoString = "PHOTO;ENCODING=B;TYPE=" + type + bean.getPHOTO();
            beanString = beanString + photoString + "\n";
        }
        if (!bean.getNOTE().isEmpty()) {
            beanString = beanString + "NOTE;CHARSET=UTF-8:" + bean.getNOTE() + "\n";
        }
        beanString = beanString + "END:VCARD";
        return beanString;
    }

    /**
     * 把beans写入文件file中
     *
     * @param file
     * @param beans
     * @throws IOException
     * @throws Exception
     */
    public static void writeFile(File file, ArrayList<UserInfo> beans) throws IOException, Exception {
        BufferedWriter out = null;
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
            out = new BufferedWriter(osw);
            System.out.println("haohaohao");
            for (int i = 0; i < beans.size(); i++) {
                UserInfo user = beans.get(i);
                String beanString = bean2String(user);
                osw.write(beanString);
                if (i != beans.size() - 1) {
                    out.newLine();
                }
            }
        } catch (FileNotFoundException ex) {
            System.out.println("系统无法打开该文件!!!(写入)");
        } finally {
            out.close();
        }
    }
}
